Why in Objective-C, we use self = [super init] instead of just [super init]?

Posted by ???? on Stack Overflow See other posts from Stack Overflow or by ????
Published on 2012-04-13T11:13:27Z Indexed on 2012/04/13 11:29 UTC
Read the original article Hit count: 174

Filed under:

In a book, I saw that if a subclass is overriding a superclass's method, we may have

self = [super init];

First, is this supposed to be done in the subclass's init method?

Second, I wonder why the call is not just

[super init];

? I mean, at the time of calling init, the memory is allocated by alloc already (I think by [Foobar alloc] where Foobar is the subclass's name. So can't we just call [super init] to initialize the member variables? Why do we have to get the return value of init and assign to self? I mean, before calling [super init], self should be pointing to a valid memory allocation chuck... so why assigning something to self again?

(if assigning, won't [super init] just return self's existing value?)

© Stack Overflow or respective owner

Related posts about objective-c